var client = {};
var app = getApp();

// 获取是否模拟开盘接口
client.isMoni = function (that) {
  my.request({
    url: app.globalData.url + "23758db9-f6ce-434b-8a40-847b246d27bc",
    method: "POST",
    dataType: "json",
    success: function (res) {
      if (res.data.succeed == 0) {
        var isMoni = true;
        console.log("是否模拟开盘 isMoni：   ", res.data.data);
        // 0 正式开盘, 1 模拟开盘
        if (res.data.data == 1) {
          isMoni = false;
        }
        my.setStorageSync({
          key: 'isMoni',
          data: isMoni
        });
        that.setData({
          // 是否模拟开盘
          isMoni: isMoni
        });
      } else {
        my.showToast({
          content: "获取是否模拟开盘失败: " + res.data.errorMsg,
          duration: 1000
        });
      }
    },
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () { }
  });
};

// 获取验证码接口
client.getCode = function (
  nameInputValue,
  certifyCardType1,
  idcardInputValue,
  phoneInputValue,
  that
) {
  my.request({
    url: app.globalData.url + "ee9183e8-fb88-4476-b346-825afc80fffc",
    method: "POST",
    data: {
      phone: phoneInputValue,
      certifyName: nameInputValue,
      certifyCardId: idcardInputValue,
      certifyCardType: certifyCardType1
    },
    dataType: "json",
    success: function (res) {
      if (res.data.succeed == 0) {
        my.showToast({
          content: "获取验证码成功",
          duration: 2000
        });
        console.log("token   ", res.data.SET_TOKEN);
        my.setStorageSync({ key: 'token', data: res.data.SET_TOKEN });
        app.globalData.token = res.data.SET_TOKEN;
      } else {
        my.showToast({
          content: "获取验证码失败: " + res.data.errorMsg,
          duration: 2000
        });
        that.setData({
          noteYesText: "获取验证码",
          isCanGetCode: true
        });
        clearInterval(interval);
      }
    },
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () { }
  });
};

// 登录接口
client.login = function (
  phoneInputValue,
  codeInputValue,
  nameInputValue,
  certifyCardType,
  idcardInputValue,
  that
) {
  my.showLoading({
    content: '加载中...'
  });
  my.request({
    url: app.globalData.url + "6545330f-315e-4644-b185-670ec8f1c00a/",
    method: "POST",
    data: {
      phone: phoneInputValue,
      code: codeInputValue,
      certifyName: nameInputValue,
      certifyCardType: certifyCardType,
      certifyCardId: idcardInputValue
    },
    headers: {
      "Access-Token": app.globalData.token
    },
    dataType: "json",
    success: function (res) {
      console.log("login  " + JSON.stringify(res));
      if (res.data.succeed == 0) {
        my.showToast({
          content: "登录成功",
          duration: 2000
        });

        var preLoginToken = res.data.data.preLoginToken;
        console.log("preLoginToken   ", preLoginToken);
        // 如果preLoginToken不为空，则此身份证号为白名单，跳过人脸识别验证
        if (certifyCardType == "0" && preLoginToken == null) {
          /**
           * 唤起认证流程
           * 参数: certifyId、url 需要通过支付宝 openapi 开放平台网关接口获取
           * 详细说明可查看文档下方的参数说明
           **/
          startAPVerify({ certifyId: res.data.data.certifyId, url: res.data.data.certifyUrl },
            function (verifyResult) {
              // 认证结果回调触发, 以下处理逻辑为示例代码，开发者可根据自身业务特性来自行处理
              if (verifyResult.resultStatus === "9000") {
                // 验证成功，接入方在此处处理后续的业务逻辑
                preLogin();
                return;
              }

              // 用户主动取消认证
              if (verifyResult.resultStatus === "6001") {
                // 可做下 toast 弱提示
                my.showToast({
                  content: "您取消了认证，请认证后才能登录",
                  duration: 2000
                });
                return;
              }
            }
          );
        } else {
          // 已登录成功
          my.setStorageSync({
            key: 'loginedTime',
            data: Date.parse(new Date())
          });
          my.navigateTo({
            url: "../buildingList/buildingList"
          });
        }
      } else {
        my.showToast({
          content: "登录失败: " + res.data.errorMsg,
          duration: 2000
        });
      }
    },
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () {
      my.hideLoading();
      that.setData({
        isLoginCanClicked: true
      });
    }
  });
};

// 唤起认证流程
function startAPVerify(options, callback) {
  my.call(
    "startBizService",
    {
      name: "open-certify",
      param: JSON.stringify(options)
    },
    callback
  );
}

// 人脸识别成功后预登录接口
function preLogin() {
  my.request({
    url: app.globalData.url + "c944c72f-716b-47e7-b894-f01be1687849/",
    method: "POST",
    headers: {
      "Access-Token": app.globalData.token
    },
    dataType: "json",
    success: function (res) {
      if (res.data.succeed == 0) {
        // 已登录成功
        my.setStorageSync({
          key: 'loginedTime',
          data: Date.parse(new Date())
        });
        my.navigateTo({
          url: "../buildingList/buildingList"
        });
      } else {
        my.showToast({
          content: "进入楼盘列表失败: " + res.data.errorMsg,
          duration: 2000
        });
      }
    },
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function (res) {
      console.log("preLogin res: " + res.data);
    }
  });
};

var interval;
// 登录页的倒计时插件
client.loginCountDown = function (that) {
  var second = 60;
  interval = setInterval(function () {
    if (second > 0) {
      console.info("获取验证码 进入倒计时 " + second);
      second--;
      that.setData({
        noteYesText: "剩余" + second + "s"
      });
    } else {
      that.setData({
        noteYesText: "获取验证码",
        isCanGetCode: true
      });
      clearInterval(interval);
      console.log("获取验证码 时间到");
    }
  }, 1000);
};

module.exports = {
  client: client
};